﻿import com.dmotiko.general.*

class ScrollContentClip
extends BaseClip {
	
	private var mcContent:MovieClip;
	private var mcMask:MovieClip;
	private var children:Array;
	private var nPos:Number;
	private var nSpace:Number;
	
	function ScrollContentClip(){
		mcContent = this.createEmptyMovieClip("mcContent", this.getNextHighestDepth());
		mcContent.setMask(mcMask);

		children = new Array();

		nPos = 0;
		nSpace = 5;
		
	}
	
	public function getScrollArea():Number {
		return mcContent._height - mcMask._height;
	}
	public function setPos(nPos:Number):Void {
		this.nPos = nPos;
		var nY = nPos * getScrollArea() / 100;
		nY = nY * -1;
		//mcContent._y = nY * -1;
		mcContent.onEnterFrame = function(){
			if( Math.abs(mcContent._y - nY) < 2){
				this._y = nY;
				delete this.onEnterFrame;
			} else {
				this._y -= (this._y - nY) /4;
			}
		}
	}
	public function getPos():Number {
		return this.nPos;
	}
	public function getChildPos(child:MovieClip):Number {
		var nHeight = mcContent._height - child._height/* - nSpace * children.length*/;
		return Math.round(child._y * 100 / nHeight);
	}
	
	public function addChild(link, data):MovieClip {
		var newChild:MovieClip = mcContent.attachMovie(link, "mcChild"+children.length, mcContent.getNextHighestDepth());
		newChild.setData(data);
		var lastChild:MovieClip = children[children.length - 1];
		if(lastChild) newChild._y = lastChild._y + lastChild._height + nSpace;
		
		children.push(newChild);
		return newChild;
	}
	
}